
if (typeof IWIW == 'undefined') IWIW = {};

IWIW.Menu = function(options) {
  // == Tulajdonságok ==
  this.options = options;
  this.items = options.items;
  this.parent = null;
  this.active = typeof options.active != 'undefined' ? options.active : 0;

  // == Metódusok ==
  this.itemClick = function(idx) {
    $(this.listItems[this.active]).removeClass('active');
    $(this.listItems[idx]).addClass('active');
    this.active = idx;
    var item = this.items[idx];
    if (typeof this.options.onChange == 'function') {
      this.options.onChange.call(this, idx, item, this.listItems[idx]);
    }
    return false;
  };
  this.attach = function(parent) {
    var that = this;
    this.parent = typeof parent == 'string' ? $('#'+parent) : parent;
    var list = $('<ul class="iwiw-menu"></ul>');
    this.listItems = [];
    for (var i=0; i<this.items.length; i++) {
      var listItem = this.listItems[i] = $('<li></li>')[0];
      $(listItem).addClass(typeof this.items[i].color != 'undefined' ? this.items[i].color : 'green');
      if (this.active == i) { $(listItem).addClass('active'); }
      var listItemLink =
        $('<a></a>')
          .attr('href', '#'+this.items[i].id)
          .attr('data-id', i)
          .text(this.items[i].title)
          .click(function(){
            return that.itemClick.call(that, [this.getAttribute('data-id')]);
          })
          .appendTo(listItem);
      $(list).append(listItem);
    }
    $(this.parent).append(list);
  };
  // == Inicializáció ==
  if (options.parent) {
    this.attach(options.parent);
  }
  if (this.options.onChange) {
    this.options.onChange.call(this, this.active, this.items[this.active], this.listItems[this.active]);
  }
};

IWIW.setTitle = function(title) {
  var h1 = document.getElementsByTagName('h1')[0];
  if (h1) {
    h1.innerHTML = title;
  }
};

IWIW.emotikonok = {
  'iwiw': "http://static.iwiw.net/common/image/emoticons/ilpic.gif",
  'cubano': "http://static.iwiw.net/common/image/emoticons/we44.gif",
  'mikulás': "http://static.iwiw.net/common/image/emoticons/we18.gif",
  'télapó': "http://static.iwiw.net/common/image/emoticons/we18.gif",
  'limuzin': "http://static.iwiw.net/common/image/emoticons/we49.gif",
  'cubana': "http://static.iwiw.net/common/image/emoticons/we45.gif",
  'földieper': "http://static.iwiw.net/common/image/emoticons/we27.gif",
  'aranypopsirét': "http://static.iwiw.net/common/image/emoticons/we33.gif",
  'łMł': "http://static.iwiw.net/common/image/emoticons/we63.gif",
  ':S': "http://static.iwiw.net/common/image/emoticons/we105.gif",
  'madárka': "http://static.iwiw.net/common/image/emoticons/we51.gif",
  'Balaenoptera': "http://static.iwiw.net/common/image/emoticons/we28.gif",
  ':P': "http://static.iwiw.net/common/image/emoticons/we104.gif",
  ':O': "http://static.iwiw.net/common/image/emoticons/we107.gif",
  'mongúz': "http://static.iwiw.net/common/image/emoticons/we50.gif",
  'képviselőfánk': "http://static.iwiw.net/common/image/emoticons/we32.gif",
  '8(': "http://static.iwiw.net/common/image/emoticons/we109.gif",
  'majom': "http://static.iwiw.net/common/image/emoticons/we26.gif",
  ':D': "http://static.iwiw.net/common/image/emoticons/we106.gif",
  'naprendszer': "http://static.iwiw.net/common/image/emoticons/we23.gif",
  'vivesbkvmegallo': "http://static.iwiw.net/common/image/emoticons/we52.gif",
  'szivecske': "http://static.iwiw.net/common/image/emoticons/we34.gif",
  'nagyfőnök': "http://static.iwiw.net/common/image/emoticons/we48.gif",
  'kiskacsa': "http://static.iwiw.net/common/image/emoticons/we20.gif",
  'rénszarvas': "http://static.iwiw.net/common/image/emoticons/we22.gif",
  'teknősbéka': "http://static.iwiw.net/common/image/emoticons/we54.gif",
  'tökfej': "http://static.iwiw.net/common/image/emoticons/we47.gif",
  ',)': "http://static.iwiw.net/common/image/emoticons/we103.gif",
  'pinkpanther': "http://static.iwiw.net/common/image/emoticons/we53.gif",
  ':*': "http://static.iwiw.net/common/image/emoticons/we110.gif",
  'gyufa': "http://static.iwiw.net/common/image/emoticons/we25.gif",
  ':)': "http://static.iwiw.net/common/image/emoticons/we101.gif",
  'csocsó': "http://static.iwiw.net/common/image/emoticons/we41.gif",
  ':(': "http://static.iwiw.net/common/image/emoticons/we102.gif",
  'kkacsafeldobjaatalpát': "http://static.iwiw.net/common/image/emoticons/we24.gif",
  'szívecske': "http://static.iwiw.net/common/image/emoticons/we34.gif",
  ':|': "http://static.iwiw.net/common/image/emoticons/we43.gif",
  'malac': "http://static.iwiw.net/common/image/emoticons/we39.gif",
  'ajándék': "http://static.iwiw.net/common/image/emoticons/we21.gif",
  'nokedli': "http://static.iwiw.net/common/image/emoticons/we31.gif",
  'tevepúp': "http://static.iwiw.net/common/image/emoticons/we29.gif",
  'kocka': "http://static.iwiw.net/common/image/emoticons/we30.gif",
  'felkelőnapháza': "http://static.iwiw.net/common/image/emoticons/we42.gif",
  'Equusburchelli': "http://static.iwiw.net/common/image/emoticons/we46.gif",
  'medveállatka': "http://static.iwiw.net/common/image/emoticons/we40.gif"
};

IWIW.emote = function(str) {
	function encodeRE(s) { return s.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$0'); }
	for (i in IWIW.emotikonok) {
		var regexp = new RegExp(encodeRE(i), "gi");
		str = str.replace(regexp, '<img src="' + IWIW.emotikonok[i] + '" class="emoticon" title="'+ i +'" alt="'+ i +'">');
	}
	return str;
}
